/*
 * COMMUNICATIONMODULEBRIDGEFROMZMQ_H_.h
 *
 *  Created on: May 11, 2014
 *      Author: root
 */

#ifndef COMMUNICATIONMODULEBRIDGEFROMZMQ_H_
#define COMMUNICATIONMODULEBRIDGEFROMZMQ_H_
#include <zmq.hpp>
#include "ICommunicationModuleBridge.h"
#include <boost/thread.hpp>
#include <queue>

using namespace zmq ;


namespace CommunicationModuleSpace {

/**
 * CommunicationModuleBridgeFromZMQ implements the ICommunicationModuleBridge using the ZMQ communication library, see ICommunicationModuleBridge
 */
class CommunicationModuleBridgeFromZMQ : public ICommunicationModuleBridge {

public:
	CommunicationModuleBridgeFromZMQ();
	~CommunicationModuleBridgeFromZMQ () ;
	bool setTypeClient ( int port , string host );
	bool setTypeServer ( int port  );
	bool transmitINITcommand(long timeout) throw (boost::thread_interrupted&);
	bool transmitGOCMD(long timeout, PassLogger * receivedDatalogger) throw (boost::thread_interrupted&);
	bool sendCommandToSat(long timeout, string commandsContent);
	bool sendENDCommand(long timeout)throw (boost::thread_interrupted&);
	bool isReadyForPass();

protected:
	bool sendAndWaitForReceive ( Message request , Message * received ) ;
	bool send(Message message);
	bool receive (Message * received) ;
	bool sendAndWaitForReceiveWithTimeout ( Message request ,Message * received ,  int timeoutsec ) ;
	bool sendWithTimeout(Message message , int timeoutsec );
	bool receiveWithTimeout (Message * received, int timeoutsec ) ;
	context_t *context ;
	socket_t *socket ;
	message_t * requestmsg;
	int connectionPort ;
	string connectionHost ;
	Logger * logger;
	bool isEndReceived;
	bool isClient;
	vector<message_t *> vec;
	bool send ( message_t *request ) ;
	message_t * createMessage_t(Message message);
	void setSocketTimeout(int opt, int timeoutsec);
	void logZMQException(string function,string content, zmq::error_t& e );

private:
	bool createConnection();
} ;

}

#endif /* COMMUNICATIONMODULEBRIDGEFROMZMQ_H_ */
